package com.yiren.dbaa.module.practice_set.line.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.line.contract.AddLineContract;
import com.yiren.dbaa.module.practice_set.site.bean.LineDataValBean;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;
import com.yiren.dbaa.net.interf.StringCallback;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/15 18:24
 * @description: 新增线路
 */
public class AddLineModel implements AddLineContract.Model {

    private Activity activity;
    private Gson gson;

    public AddLineModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }

    @Override
    public void getShareLine( String siteCode,String name,String user, CommonCallBack callBack) {
            OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.loadShareLine("?siteCode=" + siteCode+"&name="+name+"&user="+user), new StringCallback() {
                @Override
                public void getString(String result) {
                    TestBaseBean<List<LineInfoEntity>>  testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean<List<LineInfoEntity>>>() {}.getType());
                    if (testBaseBean.isSuccess()){
                        callBack.success(testBaseBean.getData());
                    }else {
                        callBack.fail("数据请求错误!");
                    }
                }

                @Override
                public void getFail() {
                    callBack.fail("数据请求失败!");
                }
            });

    }

    @Override
    public boolean isHaveData(LineInfoEntity lineInfo, List<LineInfoEntity> lineListSer) {
        boolean isHaveData = false;
        //通过名字服务器是否存在
        if (lineListSer != null){
            for (LineInfoEntity lineInfoEntity:lineListSer){
                if (lineInfo.getName().equals(lineInfoEntity.getName())){
                    isHaveData = true;
                    break;
                }
            }
        }
        //通过名字本地是否存在
        List<LineInfoEntity> lineInfoList = GradeDbController.getInstance(false).queryAll(LineInfoEntity.class);
        for (LineInfoEntity lineInfoEntity : lineInfoList) {
            if (lineInfo.getName().equals(lineInfoEntity.getName())) {
                isHaveData = true;
                break;
            }
        }
        return isHaveData;
    }
}
